package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.service.impl.SignatureValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/qingtengyun/auth")
public class QingTengYunAuthController {

    // 模拟身份认证接口
    @PostMapping()
    public String authenticate(@RequestBody User user) {
        // 这里可以根据实际传入的参数进行判断，模拟认证
        String username = user.getUsername();
        String password = user.getPassword();

        if ("zhangsan@qingteng.cn".equals(username) && "test1234".equals(password)) {
            // 返回模拟的认证数据
            return "{\n" +
                    "    \"success\": true,\n" +
                    "    \"errorCode\": null,\n" +
                    "    \"errorDesc\": null,\n" +
                    "    \"data\": {\n" +
                    "        \"comId\": \"787a5861764b3073387a\",\n" +
                    "        \"jwt\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21JZCI6Ijc4N2E1ODYxNzY0YjMwNzMzODdhIiwiaWQiOiJmOWVlODlkZGU2ZWE0ODFjYmFhZDA0YTRiMzUwNjI4ZSIsInV1aWQiOiI3ODdhNTg2MTc2NGIzMDczMzg3YSIsImlhdCI6MTczMjE3MDg1NzYyNn0.gRO97nrC7r1o0ONvqQubO-X0GzP9ah3wlf1K2ljPpY8\",\n" +
                    "        \"signKey\": \"9c3f059bc21640c2b0b5fc34458c1af8\"\n" +
                    "    }\n" +
                    "}";
        } else {
            return "{" +
                    "    \"errorCode\": 401,   //      错误码，含义与标准httpresponse的status一致\n" +
                    "    \"errorMessage\": \"\", //      错误描述\n" +
                    "    \"detail\":null\n" +
                    "}";
        }
    }

    @Autowired
    private SignatureValidationService signatureValidationService;
    @GetMapping("/your-endpoint")
    public String handleRequest(
            @RequestParam("timestamp") Long timestamp,
            @RequestParam("sign") String sign,
            @RequestParam("token") String token,
            @RequestParam("comId") String comId,
            @RequestParam Map<Object, Object> queryParams) {

        try {
            // 校验签名
            boolean isValid = signatureValidationService.validateSignature("GET", queryParams, timestamp, sign);

            if (!isValid) {
                return "签名校验失败";
            }

            return "签名校验通过 ";
        } catch (Exception e) {
            e.printStackTrace();
            return "签名验证过程中的错误: " + e.getMessage();
        }
    }

}
